package com.luych.toolbox.docker.service.controller;

import com.luych.toolbox.common.feign.intf.docker.service.DockerImageFeign;
import com.luych.toolbox.common.feign.param.docker.service.DockerImageParam;
import com.luych.toolbox.common.feign.view.docker.service.DockerImageView;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.docker.service.service.DockerImageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "镜像")
@RestController
@RequestMapping("/image")
public class DockerImageController implements DockerImageFeign {

    private final DockerImageService dockerImageService;

    @Autowired
    public DockerImageController(DockerImageService dockerImageService) {
        this.dockerImageService = dockerImageService;
    }

    @Override
    public CommonPageResult<DockerImageView> page(long pageSize, long pageNumber, String sort, String order, String serverId, String keyword) {
        return dockerImageService.page(pageSize, pageNumber, sort, order, serverId, keyword);
    }

    @Override
    public void create(String serverId, DockerImageParam model) {
        dockerImageService.create(serverId, model);
    }

    @Override
    public void remove(String serverId, String id) {
        dockerImageService.remove(serverId, id);
    }

    @Override
    public DockerImageView get(String serverId, String id) {
        return dockerImageService.get(serverId, id);
    }

    @Override
    public void push(String serverId, String id) {
        dockerImageService.push(serverId, id);
    }

    @ApiOperation("下载")
    @ApiResponses({@ApiResponse(code = 200, message = "下载成功，返回文件二进制流")})
    @GetMapping("/download")
    public void download(@ApiParam(value = "宿主机ID") @RequestParam("serverId") String serverId,
                         @ApiParam(value = "ID") @RequestParam(value = "id") String id,
                         HttpServletResponse response) {
        dockerImageService.download(serverId, id, response);
    }
}
